class <%= @model_name %> {

	<% for attr, type in @attributes %>var $<%= attr %>;
	<% end %><% if @created_at %>var $created_at;
	<% end %>
	function <%= @model_name %>($provided_db=false){
		global $db;
		if($provided_db){
			$this->db = $provided_db;
		} else if(isset($db)){
			$this->db = $db;
		} else{
			die("Erro: Banco de Dados não configurado!");
		}
	}
	
	function before_save(){
		return true;
	}
	
	function save(){
		$continue = $this->before_save();
		if(<% for attr, type in @attributes %>!$this-><%= attr %> || <% end %>$continue === false){
			return false;
		}
		<% for attr, type in @attributes %>$<%= attr %> = $this->db->escape_sql($this-><%= attr %>, "<%= type %>");
		<% end %>if(isset($this->id)){
			$query = "UPDATE <%= @table_name %> SET <%= @attributes.map{|attr, type| "#{attr}=%s"}.join(", ") %> WHERE id=" . $this->id;
		} else{
			$query = "INSERT INTO <%= @table_name %> (<%= @attributes.keys.join(", ") %><%= (@created_at) ? ", created_at" : "" %>) VALUES (<%= @attributes.map{|attr, type| "%s"}.join(", ") %><%= (@created_at) ? ", NOW()" : "" %>)";
		}
		$query = sprintf($query, <%= @attributes.map{|attr, type| "$#{attr}"}.join(", ")  %>);
		$this->db->query($query); 
		if(!isset($this->id)){
			$this->id = attribute($this->find(1, 1, "first", "ORDER BY id DESC")->id, "int");
		}
		return true;
	}
	
	function find($field=1, $value=1, $range="all", $extra_sql=""){
		$value = (is_int($value)) ? $value . "" : $this->db->escape_sql($value, "text");
		$query = "SELECT id, <%= @attributes.keys.join(", ") %><%= (@created_at) ? ", created_at" : "" %> FROM <%= @table_name %> WHERE %s = %s " . $extra_sql;
		$query = sprintf($query, $field, $value);
		$range = strtolower(trim($range));
		$records = array();
		$db_results = $this->db->query($query);
		while($object = mysql_fetch_object($db_results)){
			$record =& new <%= @model_name %>();
			$record->id = attribute($object->id, "int");
			<% for attr, type in @attributes %>$record-><%= attr %> = attribute($object-><%= attr %>, "<%= type %>");
			<% end %><% if @created_at %>$record->created_at = attribute($object->created_at, "datetime");
			<% end %>array_push($records, $record);
		}
		if($range == "first"){
			$records = $records[0];
		} else if($range != "all"){
			$records = array_slice($records, 0, intval($range));
		}
		return $records;
	}
	
	function find_by_id($id){
		if(is_int($id)){$id = $id . "";}
		$query = "SELECT id, <%= @attributes.keys.join(", ") %><%= (@created_at) ? ", created_at" : "" %> FROM <%= @table_name %> WHERE id = %s";
		$query = sprintf($query, $id);
		$record =& new <%= @model_name %>();
		$object = mysql_fetch_object($this->db->query($query));
		$this->id = $record->id = attribute($object->id, "int");
		<% for attr, type in @attributes %>$this-><%= attr %> = $record-><%= attr %> = attribute($object-><%= attr %>, "<%= type %>");
		<% end %><% if @created_at %>$this->created_at = $record->created_at = attribute($object->created_at, "datetime");
		<% end %>return $record;
	}
	
	function paginate($page, $per_page, $extra_sql=""){
		$offset = ($page - 1) * $per_page;
		$extra_sql = $extra_sql . " LIMIT " . $per_page . " OFFSET " . $offset;
		$records = $this->find(1, 1, "all", $extra_sql);
		return $records;
	}
	
	function count($extra_sql=""){
		$query = "SELECT COUNT(*) FROM posts " . $extra_sql;
		$db_results = $this->db->query($query);
		$count = mysql_fetch_row($db_results);
		$count = attribute($count[0], "int");
		return $count;
	}
	
	function delete(){
		$results = false;
		if(isset($this->id)){
			$query = "DELETE FROM <%= @table_name %> WHERE id = %s";
			$query = sprintf($query, $this->id);
			$this->db->query($query);
			$results = true;
		}
		return $results;
	}
	
}